package com.sim.simserial.ui

import android.util.Log
import android.view.View
import android.widget.AdapterView
import com.drake.channel.sendEvent
import com.drake.tooltip.toast
import com.infinit.selfhelp.common.base.ViewBindingFragment
import com.sim.serialport.library.seria.SerialManager
import com.sim.serialport.library.seria.SerialPortFinder
import com.sim.simserial.MainActivity
import com.sim.simserial.R
import com.sim.simserial.databinding.FragmentSplashBinding
import com.sim.simserial.navigation.MainNavigator
import com.sim.simserial.ui.adapter.CmdAdapter
import dagger.hilt.android.AndroidEntryPoint
import java.io.File
import javax.inject.Inject

@AndroidEntryPoint
class SplashFragment : ViewBindingFragment<FragmentSplashBinding>(R.layout.fragment_splash) {

    @Inject
    lateinit var serialManager: SerialManager

    @Inject
    lateinit var mainNavigator: MainNavigator

    private var bRate = 9600 //波特率
    private var com: String? = null


    val finder: SerialPortFinder = SerialPortFinder()
    var bb: Array<String>? = null
    var files = mutableListOf<String>()

    override fun initData() {
        super.initData()
        bb = resources.getStringArray(R.array.baudrate)
        finder.getAllDevicesPath()?.let {
            for (index in it.indices) {
                val file = File(it[index])
                Log.d("initSerialBack---", "current: ${it[index]} - canRead:${file.canRead()}, canWrite:${file.canWrite()}, canExecute:${file.canExecute()}")
                val initSerialBack = serialManager.initSerialBack(File(it[index]), bRate, 0)
                if (initSerialBack) {
                    files.add(it[index])
                }
            }
        }
    }

    override fun initView() {
        super.initView()
        val sAdapter = CmdAdapter(context, files.toTypedArray())
        val bAdapter = CmdAdapter(context, bb)
        mBinding.sSpinner.setAdapter(sAdapter)
        mBinding.bSpinner.setAdapter(bAdapter)
    }

    override fun onEvent() {
        mBinding.sSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                com = files[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
        mBinding.btnConnect.setOnClickListener {
            initSerialCall({ mainNavigator.toGoods() }, { toast("串口初始化失败") })
        }

        mBinding.btnConnectTest.setOnClickListener {
            initSerialCall({ mainNavigator.toFunctions() }, { toast("串口初始化失败") })
        }
        mBinding.btnToPrint.setOnClickListener {
            mainNavigator.toPrint()
        }
    }

    private fun initSerialCall(success: () -> Unit, fail: () -> Unit) {
        com?.let { serialPort ->
            serialManager.initSerial(requireActivity(), File(serialPort), bRate, 0) {
                if (it) {
                    sendEvent(MainActivity.RegisterSimReaderEvent())
                    success()
                } else {
                    fail()
                }
            }
        }
    }
}